import solarlunar from 'solarlunar';
window.solarlunar = solarlunar;
import formatter from './common/formatter';

/**
 *
 * @param {*} startDate 格式为 YYYY-MM-DD
 * @param {*} endDate 格式为 YYYY-MM-DD
 * @returns  Array<'YYYY-MM-DD'>
 */
window.getDaysBetween = function getDaysBetween(startDate, endDate) {
	// 将输入的日期字符串转换为 Date 对象
	const start = new Date(startDate);
	const end = new Date(endDate);
	start.setHours(0, 0, 0, 0);
	end.setHours(0, 0, 0, 0);
	// 创建一个用于迭代的日期对象，并设置为开始日期
	let currentDate = new Date(start);
	const daysArray = [];

	// 循环遍历从开始日期到结束日期的所有天数
	while (currentDate <= end) {
		// 将当前日期格式化为 YYYY-MM-DD 并添加到数组中
		const year = currentDate.getFullYear();
		const month = String(currentDate.getMonth() + 1).padStart(2, '0');
		const day = String(currentDate.getDate()).padStart(2, '0');

		daysArray.push(`${year}-${month}-${day}`);

		// 将当前日期增加一天
		currentDate.setDate(currentDate.getDate() + 1);
	}

	return daysArray;
};

/**
 * 计算农历的范围，比如，2024年农历，则数据范围为农历2023年11月20日 - 农历2024年1月20日
 */
window.NLObj = (function () {
	let today = new Date();
	let year = today.getFullYear(),
		month = today.getMonth() + 1,
		date = today.getDate();

	let lunarDateObj = solarlunar.solar2lunar(year, month, date);
	let obj = {};

	let tYear, pYear;
	if (
		(lunarDateObj.lMonth === 11 && lunarDateObj.lDay >= 20) ||
		lunarDateObj.lMonth > 11
	) {
		tYear = lunarDateObj.lYear + 1;
		pYear = lunarDateObj.lYear;
	} else {
		tYear = year;
		pYear = year - 1;
	}

	obj.collections = {
		tYear_kd: { days: 'thisYearDays', name: `nl${tYear}kuandai` },
		pYear_kd: { days: 'prevYearDays', name: `nl${pYear}kuandai` },
		tYear_ts: { days: 'thisYearDays', name: `nl${tYear}tousu` },
		pYear_ts: { days: 'prevYearDays', name: `nl${pYear}tousu` },
		pYear_kdhdl: { days: 'prevYearDays', name: `nl${pYear}kdhdl` },
	};
	// obj.collections = [
	// 	{ flag: 'ty_kd', days: 'thisYearDays', name: `nl${tYear}kuandai` },
	// 	{ flag: 'py_kd', days: 'prevYearDays', name: `nl${pYear}kaundai` },
	// 	{ flag: 'ty_ts', days: 'thisYearDays', name: `nl${tYear}tousu` },
	// 	{ flag: 'py_ts', days: 'prevYearDays', name: `nl${pYear}tousu` },
	// 	{ flag: 'py_kdhdl', days: 'prevYearDays', name: `nl${pYear}kdhdl` },
	// ];

	obj.thisYear = tYear;
	obj.prevYear = pYear;

	let startSolar1 = solarlunar.lunar2solar(obj.prevYear, 11, 21);
	let endSolar1 = solarlunar.lunar2solar(obj.thisYear, 1, 20);
	let startDate1 =
		startSolar1.cYear + '-' + startSolar1.cMonth + '-' + startSolar1.cDay;
	let endDate1 =
		endSolar1.cYear + '-' + endSolar1.cMonth + '-' + endSolar1.cDay;
	obj.thisYearDays = getDaysBetween(startDate1, endDate1).map(
		(i) => +formatter.toExcelDate(i)
	);
	obj.this_nlDays = getDaysBetween(startDate1, endDate1);

	let startSolar2 = solarlunar.lunar2solar(obj.prevYear - 1, 11, 21);
	let endSolar2 = solarlunar.lunar2solar(obj.prevYear, 1, 20);
	let startDate2 =
		startSolar2.cYear + '-' + startSolar2.cMonth + '-' + startSolar2.cDay;
	let endDate2 =
		endSolar2.cYear + '-' + endSolar2.cMonth + '-' + endSolar2.cDay;
	obj.prevYearDays = getDaysBetween(startDate2, endDate2).map(
		(i) => +formatter.toExcelDate(i)
	);
	obj.prev_nlDays = getDaysBetween(startDate2, endDate2);
	console.log(obj);
	return obj;
})();
